package com.polaris.live.popup

import android.content.Context
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ScreenUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.WeekStarAdapter
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogWeeklyStarRewardBinding
import com.polaris.live.im.sfs.bean.pusher.UserPopupNoticeMessage
import com.polaris.live.utils.route.RouterUtils

/**
 * 周星奖励弹窗
 *
 * @author Created by 天晴 on 2024/5/7 16:43
 * @since 1.0.0
 **/
class WeeklyStarRewardPopup(
    context: Context,
    private val data: UserPopupNoticeMessage,
) : CenterPopupView(context) {

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_weekly_star_reward
    }

    private lateinit var mBinding: DialogWeeklyStarRewardBinding

    val mAdapter by lazy { WeekStarAdapter() }
    override fun onCreate() {
        super.onCreate()
        mBinding = DialogWeeklyStarRewardBinding.bind(popupImplView)

        val showSize = if (data.pics.size >= 3) 3 else data.pics.size

        mBinding.apply {
            recycler.apply {
                layoutManager = GridLayoutManager(context, showSize, GridLayoutManager.VERTICAL, false)
                adapter = mAdapter
            }
            titleTv.text = data.title
            contextTv.text = data.content
            if (data.cancel != null) {
                cancel.visibility = VISIBLE
                cancel.text = data.cancel.title
                cancel.setOnSingleClickListener {
                    if (data.cancel.router?.isNotEmpty() == true) {
                        RouterUtils.routerGoto(data.cancel.router)
                    }
                    dismiss()
                }
            }
            ok.text = data.confirm.title
            ok.setOnSingleClickListener {
                if (data.confirm.router?.isNotEmpty() == true) {
                    RouterUtils.routerGoto(data.confirm.router)
                }
                dismiss()
            }
        }
        mAdapter.setList(data.pics)
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }

    companion object {

        fun showPop(context: Context, data: UserPopupNoticeMessage) {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .hasShadowBg(true)
                .asCustom(WeeklyStarRewardPopup(context, data))
                .show()
        }

    }

}